package cn.wuxing.mediation.system.service.impl;

import cn.wuxing.mediation.common.core.domain.entity.CmMediationRecord;
import cn.wuxing.mediation.common.utils.EncryptionUtils;
import cn.wuxing.mediation.common.utils.des.DESUtil;
import cn.wuxing.mediation.system.domain.vo.ApplicantVo;
import cn.wuxing.mediation.system.domain.vo.ReceptionAreaVo;
import cn.wuxing.mediation.system.mapper.IndexDisputeEventMapper;
import cn.wuxing.mediation.system.service.ISIndexService;
import cn.wuxing.mediation.system.service.ScreenService;
import cn.wuxing.mediation.system.util.Desensitization;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ISScreenServiceImpl implements ScreenService {
    @Resource
    private IndexDisputeEventMapper indexDisputeEventMapper;

    @Resource
    private ISIndexService indexService;
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DecimalFormat f = new DecimalFormat("#0.0");

    @Override
    public List<ReceptionAreaVo> getReceptionAreaTown() {
        //纠纷地图显示 1、纠纷总数 2、调解数 3、调解成功数
        List<Map<String, Object>> areaTown = indexService.getAreaTown();//区级
        List<ReceptionAreaVo> dataVos = new ArrayList<>();
//        List<ReceptionAreaVo> receptionTownNot = indexDisputeEventMapper.getReceptionAreaTownNot();
        List<ReceptionAreaVo> receptionTownOrd = indexDisputeEventMapper.getReceptionAreaTownOrd();
        List<ReceptionAreaVo> receptionTownPre = indexDisputeEventMapper.getReceptionAreaTownPre();
        List<ReceptionAreaVo> ordNotNull = receptionTownOrd.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());

        List<ReceptionAreaVo> preNotNull = receptionTownPre.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());
        long ordCount = 0;
        long ordMediationCount = 0;
        long ordMediationSuccessCount = 0;
        long preCount = 0;
        long preMediationCount = 0;
        long preMediationSuccessCount = 0;
        for (Map<String, Object> area : areaTown) {
            ordCount = 0;
            ordMediationCount = 0;
            ordMediationSuccessCount = 0;
            preCount = 0;
            preMediationCount = 0;
            preMediationSuccessCount = 0;
            ReceptionAreaVo temp = new ReceptionAreaVo();
            if (ordNotNull.size() > 0) {
                List<ReceptionAreaVo> ordTown = ordNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("townId"))
                        .collect(Collectors.toList());
                if (ordTown.size() > 0) {
                    ReceptionAreaVo ordVo = ordTown.get(0);
                    ordCount += ordVo.getCount();
                    ordMediationCount += ordVo.getMediationCount();
                    ordMediationSuccessCount += ordVo.getMediationSuccessCount();
                }
            }
            if (preNotNull.size() > 0) {
                List<ReceptionAreaVo> preTown = preNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("townId"))
                        .collect(Collectors.toList());
                if (preTown.size() > 0) {
                    ReceptionAreaVo preVo = preTown.get(0);
                    preCount += preVo.getCount();
                    preMediationCount += preVo.getMediationCount();
                    preMediationSuccessCount += preVo.getMediationSuccessCount();
                }
            }
            temp.setAreaId((Long) area.get("townId"));
            temp.setAreaName((String) area.get("townName"));
            temp.setCount(ordCount + preCount);
            temp.setMediationCount(ordMediationCount + preMediationCount);
            temp.setMediationSuccessCount(ordMediationSuccessCount + preMediationSuccessCount);
            if (area.get("centerx") != null) {
                temp.setCenterx((String) area.get("centerx"));
            } else {
                temp.setCenterx("");
            }
            if (area.get("centery") != null) {
                temp.setCentery((String) area.get("centery"));
            } else {
                temp.setCentery("");
            }
            dataVos.add(temp);
        }
        return dataVos;
    }

    @Override
    public List<ReceptionAreaVo> getReceptionAreaVillage() {
        List<Map<String, Object>> areaVillage = indexService.getAreaVillageAll();
        List<Long> villageIds = new ArrayList<>();
        areaVillage.forEach(village -> {
            villageIds.add((Long) village.get("villageId"));
        });
        List<ReceptionAreaVo> dataVos = new ArrayList<>();
//        List<ReceptionAreaVo> receptionVillageNot = indexDisputeEventMapper.getReceptionAreaTownNot();
        List<ReceptionAreaVo> receptionVillageOrd = indexDisputeEventMapper.getReceptionAreaTownOrdVillage(villageIds);
        List<ReceptionAreaVo> receptionVillagePre = indexDisputeEventMapper.getReceptionAreaTownPreVillage(villageIds);
        List<ReceptionAreaVo> ordNotNull = receptionVillageOrd.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());

        List<ReceptionAreaVo> preNotNull = receptionVillagePre.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());
        long ordCount = 0;
        long ordMediationCount = 0;
        long ordMediationSuccessCount = 0;
        long preCount = 0;
        long preMediationCount = 0;
        long preMediationSuccessCount = 0;
        for (Map<String, Object> area : areaVillage) {
            ordCount = 0;
            ordMediationCount = 0;
            ordMediationSuccessCount = 0;
            preCount = 0;
            preMediationCount = 0;
            preMediationSuccessCount = 0;
            ReceptionAreaVo temp = new ReceptionAreaVo();
            if (ordNotNull.size() > 0) {
                List<ReceptionAreaVo> ordTown = ordNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("villageId"))
                        .collect(Collectors.toList());
                if (ordTown.size() > 0) {
                    ReceptionAreaVo ordVo = ordTown.get(0);
                    ordCount += ordVo.getCount();
                    ordMediationCount += ordVo.getMediationCount();
                    ordMediationSuccessCount += ordVo.getMediationSuccessCount();
                }
            }
            if (preNotNull.size() > 0) {
                List<ReceptionAreaVo> preTown = preNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("villageId"))
                        .collect(Collectors.toList());
                if (preTown.size() > 0) {
                    ReceptionAreaVo preVo = preTown.get(0);
                    preCount += preVo.getCount();
                    preMediationCount += preVo.getMediationCount();
                    preMediationSuccessCount += preVo.getMediationSuccessCount();
                }
            }
            temp.setAreaId((Long) area.get("villageId"));
            temp.setAreaName((String) area.get("villageName"));
            temp.setCount(ordCount + preCount);
            temp.setMediationCount(ordMediationCount + preMediationCount);
            temp.setMediationSuccessCount(ordMediationSuccessCount + preMediationSuccessCount);
            if (area.get("centerx") != null) {
                temp.setCenterx((String) area.get("centerx"));
            } else {
                temp.setCenterx("");
            }
            if (area.get("centery") != null) {
                temp.setCentery((String) area.get("centery"));
            } else {
                temp.setCentery("");
            }
            dataVos.add(temp);
        }
        return dataVos;
    }

    @Override
    public List<ReceptionAreaVo> getReceptionAreaGrid() {
        List<Map<String, Object>> areaGridAll = indexService.getAreaGridAll();
        List<Long> gradIds = new ArrayList<>();
        areaGridAll.forEach(grad -> {
            gradIds.add((Long) grad.get("houseId"));
        });
        List<ReceptionAreaVo> dataVos = new ArrayList<>();
//        List<ReceptionAreaVo> receptionVillageNot = indexDisputeEventMapper.getReceptionAreaTownNot();
        List<ReceptionAreaVo> receptionGridOrd = indexDisputeEventMapper.getReceptionAreaTownOrdGrid(gradIds);
        List<ReceptionAreaVo> receptionGridPre = indexDisputeEventMapper.getReceptionAreaTownPreGrid(gradIds);
        List<ReceptionAreaVo> ordNotNull = receptionGridOrd.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());

        List<ReceptionAreaVo> preNotNull = receptionGridPre.stream()
                .filter(v -> v.getAreaId() != null)
                .filter(v -> v.getAreaName() != null)
                .filter(v -> v.getCount() != null)
                .filter(v -> v.getMediationCount() != null)
                .filter(v -> v.getMediationSuccessCount() != null)
                .collect(Collectors.toList());
        long ordCount = 0;
        long ordMediationCount = 0;
        long ordMediationSuccessCount = 0;
        long preCount = 0;
        long preMediationCount = 0;
        long preMediationSuccessCount = 0;
        for (Map<String, Object> area : areaGridAll) {
            ordCount = 0;
            ordMediationCount = 0;
            ordMediationSuccessCount = 0;
            preCount = 0;
            preMediationCount = 0;
            preMediationSuccessCount = 0;
            ReceptionAreaVo temp = new ReceptionAreaVo();
            if (ordNotNull.size() > 0) {
                List<ReceptionAreaVo> ordTown = ordNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("houseId"))
                        .collect(Collectors.toList());
                if (ordTown.size() > 0) {
                    ReceptionAreaVo ordVo = ordTown.get(0);
                    ordCount += ordVo.getCount();
                    ordMediationCount += ordVo.getMediationCount();
                    ordMediationSuccessCount += ordVo.getMediationSuccessCount();
                }
            }
            if (preNotNull.size() > 0) {
                List<ReceptionAreaVo> preTown = preNotNull.stream()
                        .filter(v -> v.getAreaId() == area.get("houseId"))
                        .collect(Collectors.toList());
                if (preTown.size() > 0) {
                    ReceptionAreaVo preVo = preTown.get(0);
                    preCount += preVo.getCount();
                    preMediationCount += preVo.getMediationCount();
                    preMediationSuccessCount += preVo.getMediationSuccessCount();
                }
            }
            temp.setAreaId((Long) area.get("houseId"));
            temp.setAreaName((String) area.get("houseName"));
            temp.setCount(ordCount + preCount);
            temp.setMediationCount(ordMediationCount + preMediationCount);
            temp.setMediationSuccessCount(ordMediationSuccessCount + preMediationSuccessCount);
            if (area.get("centerx") != null) {
                temp.setCenterx((String) area.get("centerx"));
            } else {
                temp.setCenterx("");
            }
            if (area.get("centery") != null) {
                temp.setCentery((String) area.get("centery"));
            } else {
                temp.setCentery("");
            }
            dataVos.add(temp);
        }
        return dataVos;
    }

    @Override
    public Map<String, Object> getDisputeOverall(String dateType) {
        String startTime = "";
        String endTime = "";
        Date now = new Date();
        switch (dateType) {
            case "DAY":
                endTime = simpleDateFormat.format(now);
                now.setHours(0);
                now.setMinutes(0);
                now.setSeconds(0);
                startTime = simpleDateFormat.format(now);
                break;
            case "MONTH":
                endTime = simpleDateFormat.format(now);
                now.setDate(1);
                now.setHours(0);
                now.setMinutes(0);
                now.setSeconds(0);
                startTime = simpleDateFormat.format(now);
                break;
            case "YEAR":
                endTime = simpleDateFormat.format(now);
                now.setYear(now.getYear() - 1);
                startTime = simpleDateFormat.format(now);
                break;
            case "ALL":
                break;
        }
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Long> reception = indexDisputeEventMapper.getDisputeOverall(startTime, endTime);
        Long disputeTotal = reception.get("notCount") + reception.get("ordCount") +
                reception.get("preCount");
        Long mediationTotal2 = reception.get("ordMediation2") +
                reception.get("preMediation2");
        Long mediationTotal1 = reception.get("ordMediation1") +
                reception.get("preMediation1");
        Long mediationCount = reception.get("ordMediationCount") + reception.get("preMediationCount");
        //占比
        double percentage;
        if (mediationTotal2 == 0 || mediationCount == 0) {
            percentage = 0.0;
        } else {
            percentage = (mediationTotal2 * 1.0 / mediationCount) * 100;
        }
        String resultPercentage = f.format(percentage);
        dataMap.put("disputeTotal", disputeTotal);
        dataMap.put("mediationTotal2", mediationTotal2);
        dataMap.put("mediationTotal1", mediationTotal1);
        dataMap.put("percentage", resultPercentage + "%");
        return dataMap;
    }

    @Override
    public List<Map<String, Object>> proportionOfDisputeTypes() {
        List<Map<String, Object>> disputeTypeOverview = indexService.disputeTypeOverview1("", "");
        long disputeTotal = 0l;
        //总数
        for (Map<String, Object> map : disputeTypeOverview) {
            disputeTotal += (Long) map.get("count");
        }
        //占比
        DecimalFormat f = new DecimalFormat("#0.0");
        for (Map<String, Object> map : disputeTypeOverview) {
            double percentage = ((Long) map.get("count") * 1.0 / disputeTotal) * 100;
            String resultPercentage = f.format(percentage);
            map.put("percentage", resultPercentage);
        }
        disputeTypeOverview.sort((m1, m2) ->
                Double.valueOf((String) m2.get("percentage")).compareTo(Double.valueOf((String) m1.get("percentage")))
        );

        disputeTypeOverview.forEach(map -> {
            String percentage = (String) map.get("percentage");
            map.put("percentage", percentage + "%");
        });
        if (disputeTypeOverview.size() == 0) {
            disputeTypeOverview.addAll(getDefaultValue());
            disputeTypeOverview.addAll(getDefaultValueNot());
            disputeTypeOverview.addAll(getDefaultValuePre());
        }
        return disputeTypeOverview;
    }

    private List<Map<String, Object>> getDefaultValue() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> disputeNames = indexDisputeEventMapper.getDisputeTypeOrdName();
        disputeNames.forEach(disputeName -> {
            Map<String, Object> map = new HashMap<>();
            map.put("disputeName", disputeName);
            map.put("count", 0);
            map.put("percentage", 0.0 + "%");
            list.add(map);
        });
        return list;
    }

    private List<Map<String, Object>> getDefaultValuePre() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> disputeNames = indexDisputeEventMapper.getDisputeTypePreName();
        disputeNames.forEach(disputeName -> {
            Map<String, Object> map = new HashMap<>();
            map.put("disputeName", disputeName);
            map.put("count", 0);
            map.put("percentage", 0.0 + "%");
            list.add(map);
        });
        return list;
    }

    private List<Map<String, Object>> getDefaultValueNot() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> disputeNames = indexDisputeEventMapper.getDisputeTypeNotName();
        disputeNames.forEach(disputeName -> {
            Map<String, Object> map = new HashMap<>();
            map.put("windowName", disputeName);
            map.put("count", 0);
            map.put("percentage", 0.0 + "%");
            list.add(map);
        });
        return list;
    }

    @Override
    public List<Map<String, Object>> excellentMediator() {
        List<Map<String, Object>> dataResult = new ArrayList<>();
        //获取所有的调解员
        List<Map<String, Object>> mediatorAll = indexDisputeEventMapper.getDisputeByMediatorAll();
        //非纠纷
//        List<Map<String, Object>> excellentMediatorNot = indexDisputeEventMapper.excellentMediatorNot();
        //一般纠纷
        List<Map<String, Object>> excellentMediatorOrd = indexDisputeEventMapper.excellentMediatorOrd();
        //诉前纠纷
        List<Map<String, Object>> excellentMediatorPre = indexDisputeEventMapper.excellentMediatorPre();

        List<Map<String, Object>> mediators = mediatorAll.stream()
                .filter(map -> map != null)
                .collect(Collectors.toList());
        for (Map<String, Object> mediator : mediators) {
            long count = 0;
            long mediationCount = 0;
            double percentage = 0.0;
            Map<String, Object> result = new HashMap<>();
            count = 0;
            List<Map<String, Object>> ordList = excellentMediatorOrd.stream()
                    .filter(map -> map.get("userId") == mediator.get("userId"))
                    .collect(Collectors.toList());
            List<Map<String, Object>> preList = excellentMediatorPre.stream()
                    .filter(map -> map.get("userId") == mediator.get("userId"))
                    .collect(Collectors.toList());
            Map<String, Object> ord = null;
            Map<String, Object> pre = null;
            if (ordList.size() > 0) {
                ord = ordList.get(0);
                count += (Long) ord.get("disputeCount");
                mediationCount = (Long) ord.get("mediationCount");
            }
            if (preList.size() > 0) {
                pre = preList.get(0);
                count += (Long) pre.get("disputeCount");
                mediationCount = (Long) pre.get("mediationCount");
            }
            if (mediationCount == 0 || count == 0) {
                percentage = 0.0;
            } else {
                percentage = ((mediationCount * 1.0 / count)) * 100;
            }
            String resultPercentage = f.format(percentage);
            result.put("committeeName", mediator.get("committeeName"));
            result.put("userName", mediator.get("userName"));
            result.put("disputeCount", count);
            result.put("mediationCount", mediationCount);
            result.put("percentage", resultPercentage);
            dataResult.add(result);
        }
        if (dataResult.size() > 0) {
            dataResult.sort((m1, m2) -> Double.valueOf((String) m2.get("percentage"))
                    .compareTo(Double.valueOf((String) m1.get("percentage"))));
        }
        return dataResult;
    }

    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public List<Map<String, Object>> progressFacts() {
        //1.待分派 2.受理退回 3.待受理 4.待调解 5.待二次分派 6.待二次受理 7.待二次调解 8.待结案 9.待二次结案 10.已结案
        List<Map<String, Object>> disputeProgressFactsAll = new ArrayList<>();
//        //非纠纷  描述description
//        List<Map<String, Object>> disputeProgressFactsNot = indexDisputeEventMapper.getDisputeProgressFactsNot();
        //一般纠纷
        List<Map<String, Object>> disputeProgressFactsOrd = indexDisputeEventMapper.getDisputeProgressFactsOrd();
        //诉前纠纷
        List<Map<String, Object>> disputeProgressFactsPre = indexDisputeEventMapper.getDisputeProgressFactsPre();

//        disputeProgressFactsNot.forEach(not->{
//            not.put("type","1");//非纠纷
//        });
        disputeProgressFactsOrd.forEach(ord -> {
            String name = (String) ord.get("name");
            String[] dsr = name.split(",");
            List<String> des = new ArrayList<>();
            try {
                des = Arrays.stream(dsr).map(EncryptionUtils::decrypt).collect(Collectors.toList());
            } catch (Exception ex) {
                System.out.println("解密失败");
            } finally {
                if (!des.isEmpty()) {
                    String collect = des.stream().map(str -> EncryptionUtils.mask(str, "**")).collect(Collectors.joining(","));
                    ord.put("name", collect);
                } else {
                    ord.put("name", EncryptionUtils.mask(name, "**"));
                }
            }
            ord.put("type", "2");//一般纠纷
        });
        disputeProgressFactsPre.forEach(pre -> {
            String name = (String) pre.get("name");
            String[] dsr = name.split(",");
            List<String> des = new ArrayList<>();
            try {
                des = Arrays.stream(dsr).map(EncryptionUtils::decrypt).collect(Collectors.toList());
            } catch (Exception ex) {
                System.out.println("解密失败");
            } finally {
                if (!des.isEmpty()) {
                    String collect = des.stream().map(str -> EncryptionUtils.mask(str, "**")).collect(Collectors.joining(","));
                    pre.put("name", collect);
                } else {
                    pre.put("name", EncryptionUtils.mask(name, "**"));
                }
            }
            pre.put("type", "3");//诉前纠纷
        });

//        disputeProgressFactsAll.addAll(disputeProgressFactsNot);
        disputeProgressFactsAll.addAll(disputeProgressFactsOrd);
        disputeProgressFactsAll.addAll(disputeProgressFactsPre);

        //倒序
        disputeProgressFactsAll.stream().sorted((m1, m2) -> {
            Date d1 = null;
            Date d2 = null;
            try {
                d1 = simpleDateFormat.parse((String) m1.get("time"));
                d2 = simpleDateFormat.parse((String) m2.get("time"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return d2.compareTo(d1);
        });

        long mediationCount = 0;
        String statusType = "";
        for (Map<String, Object> disputeProgressFact : disputeProgressFactsAll) {
            //登记时间
            LocalDateTime localDateTime = (LocalDateTime) disputeProgressFact.get("time");
            disputeProgressFact.put("time", dtf2.format(localDateTime.toLocalDate()));
            //调解次数
            String state = String.valueOf(disputeProgressFact.get("state"));
            if (state != null) {
                if (state.equals("5") || state.equals("6") || state.equals("7") || state.equals("9")) {
                    mediationCount = 1;
                } else {
                    mediationCount = 0;
                }
                statusType = getStatusType(state);
            } else {
                mediationCount = 0;
            }
            disputeProgressFact.put("mediationNumber", mediationCount);
            disputeProgressFact.put("state", statusType);
            String name = (String) disputeProgressFact.get("name");
            if (name != null && !(name.equals(""))) {
                int length = name.length();
                String s = name.substring(0, 1);
                if (length > 1) {
                    for (int i = 1; i < length; i++) {
                        s += "*";
                    }
                }
                disputeProgressFact.put("name", s);
            }
        }

        return disputeProgressFactsAll;
    }

    @Override
    public List<Map<String, Object>> disputeSource() {
        List<Map<String, Object>> dataResult = new ArrayList<>();
        //基层智治应用
        Map<String, Object> disputeSourceSystemNot = indexDisputeEventMapper.disputeSourceSystemNot();
        Map<String, Object> disputeSourceSystemOrd = indexDisputeEventMapper.disputeSourceSystemOrd();
        Map<String, Object> disputeSourceSystemPre = indexDisputeEventMapper.disputeSourceSystemPre();
        //TODO 浙里调解

        Map<String, Object> disputeSourceZheli = indexDisputeEventMapper.disputeSourceZheli();
//        Map<String, Object> disputeSourceZheli = new HashMap<>();
        disputeSourceZheli.put("label", "浙里调解");
//        disputeSourceZheli.put("count", 0);

        //12345
        Map<String, Object> disputeSource12345 = indexDisputeEventMapper.disputeSource12345();
        disputeSource12345.put("label", "阳光信访12345");

        //省矛盾
//        Map<String, Object> disputeSourceProvincial = new HashMap<>();
        Map<String, Object> disputeSourceProvincial = indexDisputeEventMapper.disputeSource7();
        disputeSourceProvincial.put("label", "省矛盾");
//        disputeSourceProvincial.put("count", 0);

        //矛盾秘书
        Map<String, Object> disputeSourceSecretary = indexDisputeEventMapper.disputeSource110();
//        Map<String, Object> disputeSourceSecretary = new HashMap<>();
        disputeSourceSecretary.put("label", "矛盾秘书");
//        disputeSourceSecretary.put("count", 0);
        //其他
        Map<String, Object> disputeSourceOther = indexDisputeEventMapper.disputeSource6();
//        Map<String, Object> disputeSourceOther = new HashMap<>();
        disputeSourceOther.put("label", "其他");
//        disputeSourceOther.put("count", 0);

        //组合数据
        //基层智治应用
        Map<String, Object> disputeSourceSystem = new HashMap<>();
        disputeSourceSystem.put("label", "基层智治应用");
        disputeSourceSystem.put("count", (Long) disputeSourceSystemNot.get("count") +
                (Long) disputeSourceSystemOrd.get("count") +
                (Long) disputeSourceSystemPre.get("count"));

        dataResult.add(disputeSourceSystem);
        dataResult.add(disputeSourceZheli);
        dataResult.add(disputeSource12345);
        dataResult.add(disputeSourceProvincial);
        dataResult.add(disputeSourceSecretary);
        dataResult.add(disputeSourceOther);
        return dataResult;
    }

    @Override
    public List<Map<String, Object>> windowMediationSuccess() {
        List<Map<String, Object>> result = new ArrayList<>();
        List<Map<String, Object>> windows = indexDisputeEventMapper.getWindow();
        List<Map<String, Object>> windowNot = indexDisputeEventMapper.getDisputeWindowNot();
        List<Map<String, Object>> windowOrd = indexDisputeEventMapper.getDisputeWindowOrd();
        List<Map<String, Object>> windowPre = indexDisputeEventMapper.getDisputeWindowPre();

        for (Map<String, Object> notWindow : windowNot) {
            Long windowId = (Long) notWindow.get("windowId");
            Long accepted1 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdNot(windowId);//待受理
            notWindow.put("accepted1", accepted1);
            Long accepted2 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdNotA2(windowId);//已受理
            notWindow.put("accepted2", accepted2);
        }

        for (Map<String, Object> ordWindow : windowOrd) {
            Long windowId = (Long) ordWindow.get("windowId");
            Long accepted1 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdOrd(windowId);
            ordWindow.put("accepted1", accepted1);
            Long accepted2 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdOrdA2(windowId);
            ordWindow.put("accepted2", accepted2);
        }

        for (Map<String, Object> preWindow : windowPre) {
            Long windowId = (Long) preWindow.get("windowId");
            Long accepted1 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdPre(windowId);
            preWindow.put("accepted1", accepted1);
            Long accepted2 = indexDisputeEventMapper.
                    getDisputeWindowMediationByWindowIdPreA2(windowId);
            preWindow.put("accepted2", accepted2);
        }

        long total = 0;
        long accepted1 = 0;
        long accepted2 = 0;
        for (Map<String, Object> window : windows) {
            Map<String, Object> hashMap = new HashMap<>();
            Map<String, Object> tempNot = windowNot.stream()
                    .filter(map -> map.get("windowName").equals(window.get("windowName")))
                    .collect(Collectors.toList()).get(0);
            Map<String, Object> tempOrd = windowOrd.stream()
                    .filter(map -> map.get("windowName").equals(window.get("windowName")))
                    .collect(Collectors.toList()).get(0);
            Map<String, Object> tempPre = windowPre.stream()
                    .filter(map -> map.get("windowName").equals(window.get("windowName")))
                    .collect(Collectors.toList()).get(0);
            total = (Long) tempNot.get("count") + (Long) tempOrd.get("count") + (Long) tempPre.get("count");
            accepted1 = (Long) tempNot.get("accepted1") + (Long) tempOrd.get("accepted1") + (Long) tempPre.get("accepted1");
            accepted2 = (Long) tempNot.get("accepted2") + (Long) tempOrd.get("accepted2") + (Long) tempPre.get("accepted2");
            hashMap.put("windowName", window.get("windowName"));
            hashMap.put("count", total);
            hashMap.put("accepted1", accepted1);
            hashMap.put("accepted2", accepted2);
            result.add(hashMap);
        }
        result.sort((m1, m2) ->
                ((Long) m2.get("count")).compareTo(((Long) m1.get("count")))
        );
        return result;
    }

    @Override
    public Map<String, Object> progressFactsDetail(Long eventId, String type) {
        if (type.equals("")) {
            return null;
        }
        if (eventId == null) {
            return null;
        }
        Map<String, Object> resultData = new HashMap<>();
        Map<String, Object> disputeInfo = null;
        List<ApplicantVo> applicants = null;
        List<ApplicantVo> applicant = null;
        List<ApplicantVo> applicantBei = null;
        Map<String, Object> applicantData = new HashMap<>();
        //基本信息
        switch (type) {
            case "2":
                disputeInfo = indexDisputeEventMapper.getDisputeOrdByEventId(eventId);
                break;
            case "3":
                disputeInfo = indexDisputeEventMapper.getDisputePreByEventId(eventId);
                break;
        }
        if (disputeInfo == null) {
            disputeInfo = new HashMap<>();
        }
        //申请人
        applicants = indexDisputeEventMapper.getApplicantOrdByEventId(eventId, type);
        //脱敏
        applicants.forEach(applicantVo -> {
            String name = applicantVo.getApplicantName();
            String idCard = applicantVo.getIdCard();
            String phone = applicantVo.getPhone();
            String organizeName = applicantVo.getCompanyName();
            String uscc = applicantVo.getUscc();
            String proxyUserIdCard = applicantVo.getProxyUserIdCard();
            String proxyUserPhone = applicantVo.getProxyUserPhone();
            if(null!=name) {
                if (!Desensitization.checkStrIncludeChinese(name)) {//判断名字是否是中文，未加密
                    name = DESUtil.decrypt(name);
                    if (idCard != null) {
                        idCard = DESUtil.decrypt(idCard);
                    }
                    if (phone != null) {
                        phone = DESUtil.decrypt(phone);
                    }
                }
            }
            if(null!=organizeName){
                if (!Desensitization.checkStrIncludeChinese(organizeName)) {//判断名字是否是中文，未加密
                    organizeName = DESUtil.decrypt(organizeName);
                    if (uscc != null) {
                        uscc = DESUtil.decrypt(uscc);
                    }
                }
            }
            if(null!=proxyUserIdCard){
                if (Desensitization.checkStrIncludeSpecial(proxyUserIdCard)) {
                    proxyUserIdCard = DESUtil.decrypt(proxyUserIdCard);
                    if (uscc != null) {
                        proxyUserPhone = DESUtil.decrypt(proxyUserPhone);
                    }
                }
            }
            applicantVo.setApplicantName(Desensitization.desensitizedName(name));
            applicantVo.setIdCard(Desensitization.idCardDesensitization(idCard));
            applicantVo.setPhone(Desensitization.mobilePhoneDesensitization(phone));
            applicantVo.setCompanyName(Desensitization.custNameDesensitization(organizeName));
            applicantVo.setUscc(Desensitization.idCardDesensitization(uscc));
            applicantVo.setProxyUserIdCard(Desensitization.idCardDesensitization(proxyUserIdCard));
            applicantVo.setProxyUserPhone(Desensitization.mobilePhoneDesensitization(proxyUserPhone));
        });
        //调解记录
        List<CmMediationRecord> records = indexDisputeEventMapper.getMediationHistoryByEventId(eventId, type);
        applicant = applicants.stream()
                .filter(applicantVo -> applicantVo.getApplicantCategory().equals("申请人"))
                .collect(Collectors.toList());
        applicantBei = applicants.stream()
                .filter(applicantVo -> applicantVo.getApplicantCategory().equals("被申请人"))
                .collect(Collectors.toList());
        applicantData.put("applicant", applicant);
        applicantData.put("applicantBei", applicantBei);

        resultData.put("info", disputeInfo);
        resultData.put("applicants", applicantData);
        resultData.put("records", records);
        return resultData;
    }

    @Override
    public Map<String, Object> getDisputeBodyTypeAll() {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> subjectByAdmin = indexService.getSubjectByAdmin("", "");
        dataMap.put("subjectDistribution", subjectByAdmin.get("all"));
        return dataMap;
    }


    private String getStatusType(String state) {
        //1.待分派 2.受理退回 3.待受理 4.待调解 5.待二次分派 6.待二次受理 7.待二次调解 8.待结案 9.待二次结案 10.已结案
        String status = "";
        switch (state) {
            case "1":
                status = "待分派";
                break;
            case "2":
                status = "受理退回";
                break;
            case "3":
                status = "待受理";
                break;
            case "4":
                status = "待调解";
                break;
            case "5":
                status = "待二次分派";
                break;
            case "6":
                status = "待二次受理";
                break;
            case "7":
                status = "待二次调解";
            case "8":
                status = "待结案";
                break;
            case "9":
                status = "待二次结案";
                break;
            case "10":
                status = "已结案";
                break;
        }
        return status;
    }
}
